package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.BoilHopNotes;

public class BoilHopNotesPresenter extends AbstractPresenter<BoilHopNotes> {
	final String SINGLEITEM = "boilHopNotes";
	final String MULTIITEM = "boilHopNotess";
	
	public ArrayList<BoilHopNotes> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public BoilHopNotes tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public BoilHopNotes createItemFromJSONObject(JSONObject jsonObject){
		BoilHopNotes item = new BoilHopNotes();
		try{
			item.setId(jsonObject.getInt("boilHopNoteID"));
			item.setLbs(jsonObject.getDouble("boilHopLbs"));
			item.setDate(timeFormat.parse(jsonObject.getString("boilHopTime")));
			
			HopsPresenter presenter = new HopsPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("hopID");
			item.setHop(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(BoilHopNotes item){
		try{
			JSONObject json = new JSONObject();
			json.put("boilHopNoteID", item.getId());
			json.put("boilHopLbs", item.getLbs());
			json.put("boilHopTime", getDate(item.getDate()));

			HopsPresenter presenter = new HopsPresenter();
			json.put("hopID", presenter.createJSON(item.getHop()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public BoilHopNotes getEmptyEntity(){
		BoilHopNotes entity = new BoilHopNotes();
		return entity;
	}
}
